import axios from 'axios';
export const BASEURL = process.env.VUE_APP_BASEURL;

const service: any = axios.create({
  timeout: 30000,
  transformRequest(data: any) {
    if (process.env.NODE_ENV === 'development') {
      console.log('requestData:', data);
    }
    return data;
  },
});
// 请求拦截
service.interceptors.request.use(((config: any) => {
  return config;
}), ((err: any) => {
  return Promise.reject(err);
}),
);

// 返回拦截
service.interceptors.response.use(((res: any) => {
  return res.data;
}), ((err: any) => {
  return Promise.reject(err);
}),
);

/**
 * 封装post请求
 * @param {string} url
 * @param {JSON} param
 * @param {Boolean} isCustomUrl
 * @return {Promise}
 */
export function post(data: any): Promise<any> {
  // 上传文件的文件流是无法被序列化并传递的，所以需要使用formdata对象
  /* eslint-disable */
  let formdata = new FormData();
  formdata.append('base64Str', data);
  return new Promise((resolve, reject) => {
    service({
      baseURL: '/demo/pdf/merge',
      method: 'post',
      data: formdata,
      headers: {
        'Content-Type': 'mutipart/form-data',
        'mime-type': 'octet-stream',
      },
      responseType: 'blob',
    }).then((res: any) => {
      const blob = new Blob([res], { type: 'application/pdf;charset=utf8' });
      const URL = window.URL || window.webkitURL;
      const href = URL.createObjectURL(blob);
      resolve(href);
    }).catch((err: any) => {
      reject(err);
    });
  });
}
